package gui.item;

import gui.common.Tagable;

import java.util.Date;
import java.util.GregorianCalendar;

import coreModel.HIT;
import coreModel.Item;
import coreModel.ProductGroup;

/**
 * Display data class for items.
 */
public class ItemData extends Tagable implements Comparable<ItemData>
{

	/**
	 * EntryDate attribute.
	 */
	private Date _entryDate;
	
	/**
	 * ExpirationDate attribute.
	 */
	private Date _expirationDate;
	
	/**
	 * Barcode attribute.
	 */
	private String _barcode;
	
	/**
	 * StorageUnit attribute.
	 */
	private String _storageUnit;
	
	/**
	 * ProductGroup attribute.
	 */
	private String _productGroup;
	
	private String id;

	/**
	 * Constructor.
	 * 
	 * {@pre None}
	 * 
	 * {@post getEntryDate() == current date/time}
	 * {@post getExpirationDate() == current date/time}
	 * {@post getBarcode() == ""}
	 * {@post getStorageUnit() == ""}
	 * {@post getProductGroup() == ""}
	 */
	public ItemData() {
		_entryDate = new Date();
		_expirationDate = new Date();
		_barcode = "";
		_storageUnit = "";
		_productGroup = "";
		id="";
	}


	/**
	 * Item conversion constructor.
	 * 
	 * @param item Item from which to create this ItemData
	 */
	public ItemData(Item item) 
	{
		_entryDate = item.getEntryDate();
		_expirationDate = item.getExpirationDate();
		Date entryDate = item.getEntryDate();
		int shelfLife = item.getProduct().getShelfLife();
		if (shelfLife > 0)
		{
			GregorianCalendar calendar = new GregorianCalendar();
			calendar.setTime(entryDate);
			calendar.add(GregorianCalendar.MONTH, shelfLife);
			_expirationDate = calendar.getTime();
		}
		_barcode = item.getBarcode().toString();
		_storageUnit = "";
		_productGroup = "";//item.getParentContainer().getName();
		setTag(item);
		if (item.getParentContainer() instanceof ProductGroup)
		{
			_productGroup = item.getParentContainer().getName();
			_storageUnit=HIT.getInstance().getStorageUnitManager().
				getParentStorageUnit(item.getParentContainer()).getName();
		}
		else
		{
			_productGroup = "";
			_storageUnit=HIT.getInstance().getStorageUnitManager().
						getParentStorageUnit(item.getParentContainer()).getName();
		}
	}

	/**
	 * Returns the value of the Barcode attribute.
	 */
	public String getBarcode() {
		return _barcode;
	}

	/**
	 * Sets the value of the Barcode attribute.
	 * 
	 * @param barcode New Barcode value
	 * 
	 * {@pre barcode != null}
	 * 
	 * {@post getBarcode() == barcode}
	 */
	public void setBarcode(String barcode) {
		this._barcode = barcode;
	}

	/**
	 * Returns the value of the EntryDate attribute.
	 */
	public Date getEntryDate() {
		return _entryDate;
	}

	/**
	 * Sets the value of the EntryDate attribute.
	 * 
	 * @param entryDate New EntryDate value
	 * 
	 * {@pre entryDate != null}
	 * 
	 * {@post getEntryDate() == entryDate}
	 */
	public void setEntryDate(Date entryDate) {
		this._entryDate = entryDate;
	}

	/**
	 * Returns the value of the ExpirationDate attribute.
	 */
	public Date getExpirationDate() {
		return _expirationDate;
	}

	/**
	 * Sets the value of the ExpirationDate attribute.
	 * 
	 * @param expirationDate New ExpirationDate value
	 * 
	 * {@pre None}
	 * 
	 * {@post getExpirationDate() == expirationDate}
	 */
	public void setExpirationDate(Date expirationDate) {
		this._expirationDate = expirationDate;
	}

	/**
	 * Returns the value of the StorageUnit attribute.
	 */
	public String getStorageUnit() {
		return _storageUnit;
	}

	/**
	 * Sets the value of the StorageUnit attribute.
	 * 
	 * @param storageUnit New StorageUnit value
	 * 
	 * {@pre storageUnit != null}
	 * 
	 * {@post getStorageUnit() == storageUnit}
	 */
	public void setStorageUnit(String storageUnit) {
		this._storageUnit = storageUnit;
	}

	/**
	 * Returns the value of the ProductGroup attribute.
	 */
	public String getProductGroup() {
		return _productGroup;
	}

	/**
	 * Sets the value of the ProductGroup attribute.
	 * 
	 * @param productGroup New ProductGroup value
	 * 
	 * {@pre productGroup != null}
	 * 
	 * {@post getProductGroup() == productGroup}
	 */
	public void setProductGroup(String productGroup) {
		this._productGroup = productGroup;
	}


	@Override
	public int compareTo(ItemData itemD)
	{
		Item tmpI1 = (Item)this.getTag();
		Item tmpI2 = (Item)itemD.getTag();
		
		Date d1 = tmpI1.getEntryDate();
		Date d2 = tmpI2.getEntryDate();
		
		return d1.compareTo(d2);
	}

	
	@Override
	public int hashCode()
	{
		return id.hashCode();
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		
		String other_id = ((ItemData)obj).getID();
		return id.equals(other_id);
	}
	
	@Override
	public void setTag(Object value) 
	{
		super.setTag(value);

		id = new String( ((Item)value).getBarcode().getValue() );
	}
	
	public void setID(String i)
	{
		id = new String(i);
	}
	
	public String getID()
	{
		return new String(id);
	}
}

